package com.droowa.model.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.droowa.domain.CompletePC;
import com.droowa.domain.CompletePCResult;
import com.droowa.domain.FirstCategory;
import com.droowa.domain.Product;

@Repository
public class ProductManagementDAOMybatisImpl implements ProductManagementDAO {

	@Autowired
	private SqlSessionTemplate session;

	@Override
	public int insertFirstCategory(FirstCategory firstCategory)
			throws SQLException {
		return session.insert(makeSqlId("insertFirstCategory"), firstCategory);
	}

	@Override
	public int updateFirstCategoryById(FirstCategory firstCategory)
			throws SQLException {
		return session.update(makeSqlId("updateFirstCategoryById"), firstCategory);
	}
	@Override
	public int updateProductCategoryName(Product product)
			throws SQLException {
		return session.update(makeSqlId("updateProductCategoryName"), product);
	}

	@Override
	public int deleteFirstCategoryById(String id) {
		return session.delete(makeSqlId("deleteFirstCategoryById"), id);
	}
	
	@Override
	public List<FirstCategory> selectCategoryList() throws SQLException {
		return session.selectList(makeSqlId("selectCategoryList"));
	}
	
	@Override
	public List<FirstCategory> selectFixedCategoryList() throws SQLException {
		return session.selectList(makeSqlId("selectFixedCategoryList"));
	}
	
	@Override
	public List<FirstCategory> selectUnFixedCategoryList() throws SQLException {
		return session.selectList(makeSqlId("selectUnFixedCategoryList"));
	}

	@Override
	public FirstCategory selectFirstCategoryById(String categoryId) throws SQLException {
		return session.selectOne(makeSqlId("selectFirstCategoryById"), categoryId);
	}

	@Override
	public int insertProduct(Product product) throws SQLException {
		return session.insert(makeSqlId("insertProduct"), product);
	}

	@Override
	public int updateProduct(Product product) throws SQLException {
		return session.update(makeSqlId("updateProduct"), product);
	}

	@Override
	public int deleteProductById(String id) throws SQLException {
		return session.delete(makeSqlId("deleteProductById"), id);
	}

	@Override
	public List<Product> selectProductList() throws SQLException {
		return session.selectList(makeSqlId("selectProductList"));
	}
	
	@Override
	public List<Product> selectRemovedProductList() throws SQLException {
		return session.selectList(makeSqlId("selectRemovedProductList"));
	}
	
	@Override
	public List<Product> selectProductListPaging(int page) throws SQLException {
		return session.selectList(makeSqlId("selectProductListPaging"), page);
	}
	
	@Override
	public List<Product> selectRemovedProductListPaging(int page) throws SQLException {
		return session.selectList(makeSqlId("selectRemovedProductListPaging"), page);
	}
	
	@Override
	public int selectRowCountPL() throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountPL"));
		return cnt;
	}
	@Override
	public int selectRowCountRPL() throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountRPL"));
		return cnt;
	}
	@Override
	public Product selectProductById(String id) throws SQLException {
		return session.selectOne(makeSqlId("selectProductById"), id);
	}

	@Override
	public List<Product> selectProductByName(String name, int page) throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("name", name);
		input.put("page", page);
		return session.selectList(makeSqlId("selectProductByName"), input);
	}
	
	@Override
	public int selectRowCountPN(String name) throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountPN"), name);
		return cnt;
	}
	@Override
	public List<Product> selectProductByBrand(String brand) throws SQLException {
		return session.selectList(makeSqlId("selectProductByBrand"), brand);
	}
	
	@Override
	public List<Product> selectProductByBrandPaging(String brand, int page) throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("brand", brand);
		input.put("page", page );
		return session.selectList(makeSqlId("selectProductByBrandPaging"), input);
	}

	@Override
	public int selectRowCountB(String brand) throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountB"), brand);
		return cnt;
	}
	
	@Override
	public List<Product> selectProductByModelNo(String modelNo, int page) throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("modelNo", modelNo);
		input.put("page", page );
		return session.selectList(makeSqlId("selectProductByModelNo"), input);
	}

	@Override
	public int selectRowCountM(String modelNo) throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountM"), modelNo);
		return cnt;
	}
	
	@Override
	public List<Product> selectProductByPriceRange(int sPrice, int ePrice, int page)
			throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("s", sPrice);
		input.put("e", ePrice);
		input.put("page", page);
		return session.selectList(makeSqlId("selectProductByPriceRange"), input);
	}
	@Override
	public int selectRowCountPR(int sPrice, int ePrice) throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("s", sPrice);
		input.put("e", ePrice);
		int cnt = session.selectOne(makeSqlId("selectRowCountPR"), input);
		return cnt;
	}
	@Override
	public List<Product> selectRProductByName(String name, int page) throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("name", name);
		input.put("page", page);
		return session.selectList(makeSqlId("selectRProductByName"), input);
	}
	
	@Override
	public int selectRowCountRPN(String name) throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountRPN"), name);
		return cnt;
	}

	@Override
	public List<Product> selectRProductByBrand(String brand) throws SQLException {
		return session.selectList(makeSqlId("selectRProductByBrand"), brand);
	}
	@Override
	public List<Product> selectRProductByBrandPaging(String brand, int page) throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("brand", brand);
		input.put("page", page );
		return session.selectList(makeSqlId("selectRProductByBrandPaging"), input);
	}

	@Override
	public int selectRowCountRB(String brand) throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountRB"), brand);
		return cnt;
	}
	@Override
	public List<Product> selectRProductByModelNo(String modelNo, int page) throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("modelNo", modelNo);
		input.put("page", page );
		return session.selectList(makeSqlId("selectRProductByModelNo"), input);
	}

	@Override
	public int selectRowCountRM(String modelNo) throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountRM"), modelNo);
		return cnt;
	}

	@Override
	public List<Product> selectRProductByPriceRange(int sPrice, int ePrice, int page)
			throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("s", sPrice);
		input.put("e", ePrice);
		input.put("page", page);
		return session.selectList(makeSqlId("selectRProductByPriceRange"), input);
	}
	@Override
	public int selectRowCountRPR(int sPrice, int ePrice) throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("s", sPrice);
		input.put("e", ePrice);
		int cnt = session.selectOne(makeSqlId("selectRowCountRPR"), input);
		return cnt;
	}
	@Override
	public int updateDeltoTrue(Product product) throws SQLException {
		return session.update(makeSqlId("updateDelToTrue"), product);
	}

	@Override
	public int updateDeltoFalse(Product product) throws SQLException {
		return session.update(makeSqlId("updateDelToFalse"), product);
	}

	@Override
	public List<Product> selectInventoryList(int page)
			throws SQLException {
		return session.selectList(makeSqlId("selectInventoryList"), page);
	}
	
	@Override
	public int updateSafeInventory(Product product) throws SQLException {
		return session.update(makeSqlId("updateSafeInventory"), product);
	}
	
	@Override
	public List<Product> selectProductByCategoryName(String categoryName, int page)
			throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("categoryName", categoryName);
		input.put("page", page );
		return session.selectList(makeSqlId("selectProductByCategoryName"), input);
	}
	
	@Override
	public int selectRowCountCN(String categoryName) throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountCN"), categoryName);
		return cnt;
	}
	
	@Override
	public List<Product> selectRProductByCategoryName(String categoryName, int page)
			throws SQLException {
		HashMap<String, Object> input = new HashMap<String, Object>();
		input.put("categoryName", categoryName);
		input.put("page", page );
		return session.selectList(makeSqlId("selectRProductByCategoryName"), input);
	}
	@Override
	public int selectRowCountRCN(String categoryName) throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountRCN"), categoryName);
		return cnt;
	}
	@Override
	public int insertCompletePC(CompletePC completePC) throws SQLException {
		return session.insert(makeSqlId("insertCompletePC"), completePC);
	}

	@Override
	public List selectCompletePCList() throws SQLException {
		return session.selectList(makeSqlId("selectCompletePCList"));
	}
	
	@Override
	public List selectCompleteNameById(String id)
			throws SQLException {	
		return session.selectList(makeSqlId("selectCompleteNameById"), id);
	}

	@Override
	public List selectCompletePC(String completeId) throws SQLException {
		return session.selectList(makeSqlId("selectCompletePC"), completeId);
	}

	@Override
	public List<CompletePC> selectCompletePCListPaging(int page)
			throws SQLException {
		return session.selectList(makeSqlId("selectCompletePCListPaging"), page);
	}
	
	@Override
	public int selectRowCountCPL() throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountCPL"));
		return cnt;
	}
	@Override
	public List<CompletePC> selectCompleteEventPaging(int page)
			throws SQLException {
		return session.selectList(makeSqlId("selectCompleteEventPaging"), page);
	}
	
	@Override
	public int selectRowCountEPL() throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountEPL"));
		return cnt;
	}
	@Override
	public CompletePC selectCompleteDomain(String completeId)
			throws SQLException {
		return session.selectOne(makeSqlId("selectCompleteDomain"), completeId);
	}
	
	@Override
	public Map<String, String> selectCompletePCById(String completeId)
			throws SQLException {
		return session.selectOne(makeSqlId("selectCompletePCById"), completeId);
	}

	@Override
	public List<CompletePCResult> selectCompletePCByPrice(int sPrice, int ePrice)
			throws SQLException {
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("sPrice", sPrice);
		map.put("ePrice", ePrice);
		return session.selectList(makeSqlId("selectCompletePCByPrice"), map);
	}
	
//	@Override
//	public List<CompletePCResult> selectCompletePCByCPU(String cpu)
//			throws SQLException {
//		return session.selectList(makeSqlId("selectCompletePCByCPU"), cpu);
//	}

	@Override
	public List<CompletePCResult> selectCompletePCByLow(int page)
			throws SQLException {
		return session.selectList(makeSqlId("selectCompletePCByLow"), page);
	}
	@Override
	public int selectRowCountLP() throws SQLException {
		int cnt = session.selectOne(makeSqlId("selectRowCountLP"));
		return cnt;
	}
	
	@Override
	public List selectCompletePCByCPUBrand(String cpu, String brand)
			throws SQLException {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("cpu", cpu);
		map.put("brand", brand);
		return session.selectList(makeSqlId("selectCompletePCByCPUBrand"), map);
	}

	@Override
	public List<CompletePCResult> selectCompletePCByHigh(int page)
			throws SQLException {
		return session.selectList(makeSqlId("selectCompletePCByHigh"), page);
	}
	@Override
	public int updateCompletePC(CompletePC completePC) throws SQLException {
		return session.update(makeSqlId("updateCompletePC"), completePC);
	}


	@Override
	public int deleteCompletePCById(String completeId) throws SQLException {
		return session.delete(makeSqlId("deleteCompletePCById"), completeId);
	}
	
	@Override
	public int updateEventToTrue(CompletePC completePC) throws SQLException {
		return session.update(makeSqlId("updateEventToTrue"), completePC);
	}
	
	@Override
	public int updateEventToFalse(CompletePC completePC) throws SQLException {
		return session.update(makeSqlId("updateEventToFalse"), completePC);
	}
	
	@Override
	public List<CompletePC> selectEventList() throws SQLException {
		return session.selectList(makeSqlId("selectEventList"));
	}

	
	@Override
	public List<CompletePCResult> selectCompletePCByCPUPaging(int page)
			throws SQLException {
		return session.selectList(makeSqlId("selectCompletePCByCPUPaging"), page);
	}
	@Override
	public List<CompletePCResult> selectCompletePCByGRAPHICPaging(int page)
			throws SQLException {
		return session.selectList(makeSqlId("selectCompletePCByGRAPHICPaging"), page);
	}

	
	@Override
	public Map selectCompletePCByIdPrice(String completeId) {
		return session.selectOne(makeSqlId("selectCompletePCByIdPrice"), completeId);
	}

	//완제품 판매량++
	@Override
	public void updateCompletePCCnt(String completeId, int cnt) {
		HashMap map = new HashMap();
		map.put("cnt", cnt);
		map.put("completeId", completeId);
		session.update(makeSqlId("updateCompletePCCnt"), map);
		
	}
	
	@Override
	public CompletePC selectCompletePCByIdBasic(String completeId) {
		return session.selectOne(makeSqlId("selectCompletePCByIdBasic"), completeId);
	}
	
	@Override
	public void updateProductQuantity(String id) {
		session.update(makeSqlId("updateProductQuantity"), id);
	}

	private String makeSqlId(String id){
		return "droowa.product."+id;
	}

}
	
